console.log('this is  myAccount.js');
var _account_service = require('service/account-service.js');

require('page/common/header/index.js');
var myAccount = {
    username : "",
    init: function () {
        this.bindEvents();
        this.loadAccountInfo();
        this.upOrDown();
        return this;
    },
    //点击事件
    bindEvents: function () {
        var _this = this;
        //检验密码
        $('#originalPassword').on('keyup', function () {
            if (this.value.length < 8 && this.value.length > 2) {
                $('#originalPasswordTips').attr("class", null).text('');
            }
            else {
                $('#originalPasswordTips').attr("class", 'errorMsg').text('密码长度大于2不超过7');
            }
        });
        $('#newPassword').on('keyup', function () {

            if (this.value.length < 8 && this.value.length > 2) {
                $('#passwordTips').attr("class", 'successMsg').text('密码可用');
            }
            else {
                $('#passwordTips').attr("class", 'errorMsg').text('密码长度大于2不超过7');
            }
        });
        $('#repeatedPassword').on('keyup', function () {
            if (this.value === $('#newPassword').val()) {
                console.log("yes");
                $('#repeatedPasswordTips').attr("class", null).text('');
            }
            else {
                console.log("no");
                $('#repeatedPasswordTips').attr("class", 'errorMsg').text('密码不一致');
            }
        });
        //修改密码
        $('#updateSignOn').on('click', function () {
            if ($('#newPassword').val() === $('#repeatedPassword').val()) {
                var account = {
                    username: $('#username').val(),
                    curPw   : $('#originalPassword').val(),
                    newPw   : $('#newPassword').val(),
                    //repPw   : $('#repeatedPassword').val(),
                };
                console.log(account.username);
                console.log(account.password);
                _account_service.updatePassword(
                    account,
                    function (resp) {
                        $('#originalPassword','#newPassword', '#repeatedPassword').val('');
                        alert('密码修改成功!');
                    },
                    function (errorMsg) {
                        alert('密码修改失败!');
                    }
                );
            }else {
                alert("密码不一致");
            }
        });
        //更新信息
        $('#updateAll').click (
            function () {
                _account_service.update(
                   $('#updateAccount').serialize(),
                    function (resp) {
                       _this.showMessage(resp);
                       alert('账户修改成功!');

                    },
                    function (errorMsg) {
                       alert('密码修改失败!');
                    }
                )
            }
        );
    },
    loadAccountInfo: function () {
        var _this = this;
        _account_service.checkLogin(
            //成功调用//res 为用户
            function (res) {
                console.log('res:' + res.username);
                alert("欢迎用户:" + res.username);
                    username=res.username;
                    console.log('res:' + res.username +'this'+username);
                    $('#username').val(res.username);           
                    $('#firstName').val(res.firstName);
                    $('#lastName').val(res.lastName);
                    $('#email').val(res.email);
                    $('#phone').val(res.phone);
                    $('#address1').val(res.address1);
                    $('#address2').val(res.address2);
                    $('#city').val(res.city);
                    $('#state').val(res.state);
                    $('#zip').val(res.zip);
                    $('#country').val(res.country);
                    $('#languagePreference').val(res.languagePreference);
                    $('#favouriteCategoryId').val(res.favouriteCategoryId);
                    $('#listOption').attr('checked', res.listOption);
                    $('#bannerOption').attr('checked', res.bannerOption);
            },
            //失败调用
            function (errMsg) {
                console.log('errrrrr');
                window.location.href = './account-login.html';
            }
        );
    },
    upOrDown : function () {
        _account_service.checkLogin(
            //成功调用//res 为用户
            function (res) {
                console.log('upOrDown:' + res.username +'this'+username);
                var a = document.getElementById("myOrders"); 
                a.href = "./order-myOrders.html?id=toMyAccount&route="+res.username;
            },
            //失败调用
            function (errMsg) {
                console.log('errrrrr');
            }
        );
    }
}
module.exports = myAccount.init();